package com.h5lib.nchuoffice.app.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.h5lib.nchuoffice.CacheManager;
import com.h5lib.nchuoffice.NchuOfficeUtils;
import com.h5lib.nchuoffice.app.cfg.Config;
import com.h5lib.nchuoffice.app.interceptor.AuthInterceptor;
import com.h5lib.nchuoffice.model.UserInfo;
import com.h5lib.server.ModelMap;
import com.h5lib.server.annotation.Controller;
import com.h5lib.server.annotation.RequestMapping;

import fi.iki.elonen.NanoHTTPD.IHTTPSession;
import fi.iki.elonen.NanoHTTPD.Response;
import fi.iki.elonen.NanoHTTPD.Response.Status;

@Controller
public class LoginAction {
	
	@RequestMapping(value="/login.html")
	public String login(IHTTPSession session, Response response ,ModelMap modelMap){

		return "/ui/login.html";
	}
	
	@RequestMapping(value="/logout.html")
	public String logout(IHTTPSession session, Response response ,ModelMap modelMap){
		CacheManager.clear();
		session.getCookies().delete(AuthInterceptor.AUTH_KEY);
		return "/ui/login.html";
	}
	
	@RequestMapping(value="/doLogin")
	public Response doLogin(IHTTPSession session, Response response ,ModelMap modelMap){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> parms = session.getParms();
			if("test".equals(parms.get("user"))){
				//测试账号使用缓存
				return doOffLogin(session, response, modelMap);
			}
			UserInfo info = NchuOfficeUtils.login(parms.get("user"), parms.get("password"));
			if(info != null){
				//将用户信息缓存
				CacheManager.addCache(Config._CACHE_USERINFO, info);
				map.put("err", "0");
				map.put("msg", "登录成功");
				map.put("user", info);
				session.getCookies().set(AuthInterceptor.AUTH_KEY, AuthInterceptor.getAuthValue(), 3600*100);
			}
			else{
				map.put("err", "-1");
				map.put("msg", "登录失败");
			}
		} catch (IOException e) {
			map.put("err", "-2");
			map.put("msg", "系统正忙");
		}
		return new Response(Status.OK, "text/plain", JSON.toJSONString(map));
	}
	
	@RequestMapping(value="/doHybridLogin")
	public Response doHybridLogin(IHTTPSession session, Response response ,ModelMap modelMap){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> parms = session.getParms();
			if("test".equals(parms.get("user"))){
				//测试账号使用缓存
				return doOffLogin(session, response, modelMap);
			}
			//院校和班级从缓存获取
			CacheManager.loadCacheFromDbByPrefixs(new String[]{Config._CACHE_BASEDATA,Config._CACHE_CLASSES});
			UserInfo info = NchuOfficeUtils.login(parms.get("user"), parms.get("password"));
			if(info != null){
				//将用户信息缓存
				CacheManager.addCache(Config._CACHE_USERINFO, info);
				map.put("err", "0");
				map.put("msg", "登录成功");
				map.put("user", info);
				session.getCookies().set(AuthInterceptor.AUTH_KEY, AuthInterceptor.getAuthValue(), 3600*100);
			}
			else{
				map.put("err", "-1");
				map.put("msg", "登录失败");
			}
		} catch (IOException e) {
			map.put("err", "-2");
			map.put("msg", "系统正忙");
		}
		return new Response(Status.OK, "text/plain", JSON.toJSONString(map));
	}
	
	@RequestMapping(value="/doOffLogin")
	public Response doOffLogin(IHTTPSession session, Response response ,ModelMap modelMap){
		Map<String, Object> map = new HashMap<String, Object>();
		CacheManager.loadCacheFromDb();
		UserInfo info = (UserInfo) CacheManager.getCache(Config._CACHE_USERINFO);
		if(info != null){
			//将用户信息缓存
			CacheManager.addCache(Config._CACHE_USERINFO, info);
			map.put("err", "0");
			map.put("msg", "登录成功");
			map.put("user", info);
			session.getCookies().set(AuthInterceptor.AUTH_KEY, AuthInterceptor.getAuthValue(), 3600*100);
		}
		else{
			map.put("err", "-1");
			map.put("msg", "未找到缓存记录，登录失败");
		}
		return new Response(Status.OK, "text/plain", JSON.toJSONString(map));
	}
	
}
